<template>
  <el-dialog :title="!dataForm.brandId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <!-- label-width 调整表单项lable的宽度 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="120px">
      <el-form-item label="品牌名" prop="name">
        <el-input v-model="dataForm.name" placeholder="品牌名"></el-input>
      </el-form-item>
      <el-form-item label="品牌logo地址" prop="logo">
        <!-- <el-input v-model="dataForm.logo" placeholder="品牌logo地址"></el-input> -->
        <SingleUpload v-model="dataForm.logo"></SingleUpload>
        <!-- 或者 <single-upload></single-upload> -->
      </el-form-item>
      <el-form-item label="介绍" prop="descript">
        <el-input v-model="dataForm.descript" placeholder="介绍"></el-input>
      </el-form-item>
      <el-form-item label="显示状态" prop="showStatus">
        <el-switch v-model="dataForm.showStatus" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
        :inactive-value="0">
        </el-switch>
      </el-form-item>

      <el-form-item label="检索首字母" prop="firstLetter">
        <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <!-- v-model.number 修饰符 (modifier) 表示是数字 -->
        <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

//@ 表示src目录  src/components/upload
import SingleUpload from "@/components/upload/singleUpload"

export default {
  components:{SingleUpload},
  data() {

    //自定义校验 首字母为英文,且只有一个   参数(校验规则 ,当前的值, 成功失败的回调)
    function validateFirstLetter(rule, value, callback) {
      if (value === '') {
        callback(new Error('检索首字母为空,必须填写'));
      } else if (!/^[a-zA-z]$/.test(value)) {
        //正则判断字母 在 A-Z的范围内
        callback(new Error('字母需要在A-Z的范围内'));
      } else {
        callback();
      }

    }
    //校验排序字段
    function validateSort(rule, value, callback) {
      console.log("value sort : " , value);
      console.log("value sort boo : " , Number.isInteger(value));
      
      if (value === '') {
        callback(new Error('排序字段为空,必须填写'));
      } else if (!Number.isInteger(value) || value < 0) {
        //需要再表单上进行修饰符v-model.number,  isInteger()才生效
        //排序字段需要时整数 且 大于0
        callback(new Error('请输入数字值且大于0'));
      } else {
        callback();
      }
    }

    return {
      visible: false,
      dataForm: {
        brandId: 0,
        name: '',
        logo: '',
        descript: '',
        showStatus: '',
        firstLetter: '',
        sort: ''
      },
      dataRule: {
        //自定义校验 校验规则 当前的值 成功失败的回调
        name: [
          { required: true, message: '品牌名不能为空', trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '品牌logo地址不能为空', trigger: 'blur' }
        ],
        descript: [
          { required: true, message: '介绍不能为空', trigger: 'blur' }
        ],
        showStatus: [
          { required: true, message: '显示状态[0-不显示；1-显示]不能为空', trigger: 'blur' }
        ],
        //自定义校验  
        firstLetter: [
          { validator: validateFirstLetter, trigger: 'blur' }
        ],
        sort: [
          { validator: validateSort, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.brandId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.brandId) {
          this.$http({
            url: this.$http.adornUrl(`/product/brand/info/${this.dataForm.brandId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.data.name
              this.dataForm.logo = data.data.logo
              this.dataForm.descript = data.data.descript
              this.dataForm.showStatus = data.data.showStatus
              this.dataForm.firstLetter = data.data.firstLetter
              this.dataForm.sort = data.data.sort
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/brand/${!this.dataForm.brandId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'brandId': this.dataForm.brandId || undefined,
              'name': this.dataForm.name,
              'logo': this.dataForm.logo,
              'descript': this.dataForm.descript,
              'showStatus': this.dataForm.showStatus,
              'firstLetter': this.dataForm.firstLetter,
              'sort': this.dataForm.sort
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
